import axios from 'axios'
export default {
  getUsers ({commit, state}) {
    // 发起请求前让loading显示
    commit('toggleLoading', true)
    const {page, pagesize, name} = state.search
    axios.get('http://localhost:3000/user/listpage', {
      params: {
        page,
        pagesize,
        name
      }
    }).then(res => {
      const {users, total} = res.data.users
      // 请求成功，成功后，将对应的数据提交给mutation
      commit('setUsers', users)
      commit('setSearch', {total})
      commit('toggleLoading', false)
    })
  },
  removeUser ({commit}, payload) {
    commit('toggleLoading', true)
    const {id, index} = payload
    axios.get('http://localhost:3000/user/remove', {
      params: {
        id
      }
    }).then(res => {
      if (res.data.code === 200) {
        console.log('成功')
        // 提交index到mutation
        commit('removeUser', index)
      }
      commit('toggleLoading', false)
    })
  },
  removeUsers ({commit, dispatch, state}) {
    commit('toggleLoading', true)
    axios.get('http://localhost:3000/user/batchremove', {
      params: {
        ids: state.ids
      }
    }).then(res => {
      if (res.data.code === 200) {
        // dispatch('getUsers') 
        // 根据ids直接删除users中对应ids的用户
        // const ids = state.ids.split(',')
        commit('removeUsers')


      }
      commit('toggleLoading', false)
    })
  }
}